package com.istabs;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.istabs.beans.Categoria;
import com.istabs.common.UIHelper;
import com.istabs.services.DirectorioService;

public class IstabsActivity extends ListActivity {
	private Categoria[] establecimientos;
	private TipoEstablecimientoAdapter tipoEstablecimientoAdapter;
	private DirectorioService service;
	private ProgressDialog dialog;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		service = new DirectorioService();
		try {
			establecimientos = service.obtenerCategorias(null);
			tipoEstablecimientoAdapter = new TipoEstablecimientoAdapter(this,
					R.layout.directorio_item,
					R.id.txtNombreCategoriaEstablecimiento, establecimientos);
			setListAdapter(tipoEstablecimientoAdapter);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private class TipoEstablecimientoAdapter extends ArrayAdapter<Categoria> {

		public TipoEstablecimientoAdapter(Context context, int layoutId,
				int textViewResourceId, Categoria[] objects) {
			super(context, layoutId, textViewResourceId, objects);
			// TODO Auto-generated constructor stub
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = super.getView(position, convertView, parent);
			TextView tv = (TextView) view
					.findViewById(R.id.txtNombreCategoriaEstablecimiento);
			tv.setText(getItem(position).getNombre());
			return view;
		}

	}

	@Override
	public void onDestroy() {
		if (isFinishing()) {
			Log.d(this.getClass().getSimpleName(), "Destroying View ...");
			UIHelper.killApp(true);
		}
		super.onDestroy();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		int item = tipoEstablecimientoAdapter.getItem(position).getId();
		final RequestTask t = new RequestTask();
		dialog = ProgressDialog.show(this, "Istabs",
				"Cargando.\n Por favor espere...", true, true,
				new OnCancelListener() {

					
					public void onCancel(DialogInterface dialog) {
						t.cancel(true);
					}
				});
		t.execute(item);

	}

	private class RequestTask extends AsyncTask<Integer, Integer, Categoria[]> {
		private Intent intent;

		protected Categoria[] doInBackground(Integer... fechas) {
			DirectorioService service = new DirectorioService();
			try {
				intent = new Intent(IstabsActivity.this,
						ListaTipoEstablecimientosActivity.class);
				return service.obtenerCategorias(fechas[0].toString());

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}

		}

		protected void onProgressUpdate(Integer... progress) {

		}

		protected void onPostExecute(Categoria[] result) {
			if (result != null) {

				intent.putExtra("categorias", result);
				startActivity(intent);
				dialog.dismiss();
			} else {
				dialog.dismiss();
				// prepare the alert box
				AlertDialog.Builder alertbox = new AlertDialog.Builder(
						IstabsActivity.this);
				// set the message to display
				alertbox.setMessage("Error al cargar los eventos. Intente mas tarde");
				alertbox.show();
			}

		}
	}

}